import axios from "axios";
import {getToken} from "./auth";
import {Message, MessageBox} from 'element-ui'

//创建axios实例
const service = axios.create({
  baseURL: process.env.BASE_API, // api的base_url
  timeout: 60000 // 请求超时时间
});

service.interceptors.request.use(config => {
  // Do something before request is sent
  // 在此处封装头部会使请求出现仅有次头部的问题而导致一些请求失效
  // config.headers['Content-Type'] = 'application/json;charset=UTF-8';
  config.headers['Access-Control-Expose-Headers'] = 'Authorization';
  config.headers['Authorization'] = getToken()
  return config;
}, error => {
  //当请求出错时我们需要处理的逻辑可以写在这里
  console.log(error); // for debug
  //报错后抛出异常 我们需要捕获异常再处理
  Promise.reject(error);
});

service.interceptors.response.use(
  response => {
    return response.data;
  },
  error => {
    console.log('err' + error.msg); // for debug
    Message({
      message: error.msg,
      type: 'error',
      duration: 3 * 1000
    })
    return Promise.reject(error);
  }
)

export default service;
